<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 商品出售信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
          >批量删除</el-button
        > -->
        <el-input
          v-model="query.bindUser"
          placeholder="转让人"
          class="handle-input mr10"
        ></el-input>
        <el-input
          v-model="query.region"
          placeholder="求租区域"
          class="handle-input mr10"
        ></el-input>
        <el-select v-model="value" placeholder="状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @click="statusShow(value)"
          >
          </el-option>
        </el-select>&nbsp;
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button>&nbsp;
        <el-button type="primary" @click="addSearch"
          >添加</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        height="500px"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="40"
          align="center"
        ></el-table-column>
        <el-table-column
          property="id"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          property="bindUser"
          label="转让人"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          property="region"
          label="求租区域"
          width="82"
          align="center"
        ></el-table-column>
        <el-table-column
          property="telephone"
          label="电话"
          width="110"
          align="center"
        ></el-table-column>
        <el-table-column
          property="mode"
          label="录入方式"
          width="110"
          align="center"
        ></el-table-column>
        <el-table-column
          property="tittle"
          label="信息标题"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          property="address"
          label="详细地址"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          width="55"
          label="状态"
          align="status"
          :formatter="fgf"
        >
        </el-table-column>

        <el-table-column
          property="createdDate"
          label="创建时间"
        ></el-table-column>
        <el-table-column
          property="modifiedDate"
          label="更新时间"
        ></el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
             <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            > 
           <!--  <el-button type="text" @click="open(scope.$index, scope.row)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" v-model="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="转让人">
          <el-input v-model="form.bindUser"></el-input>
        </el-form-item>
        <el-form-item label="求租区域">
          <el-input v-model="form.region"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.telephone"></el-input>
        </el-form-item>
        <el-form-item label="录入方式">
          <el-input v-model="form.mode"></el-input>
        </el-form-item>
        <el-form-item label="信息标题">
          <el-input v-model="form.tittle"></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status" size="medium">
            <el-radio v-for="(item, index) in StatusOptions" :key="index" :label="item.value"
            :disabled="item.disabled">{{item.label}}</el-radio></el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveEdit">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 添加弹出框 -->
    <el-dialog title="添加" v-model="resetForm" width="30%">
      <el-form ref="addform" :model="addform" label-width="70px">
        <el-form-item label="转让人" prop="bindUser">
          <el-input v-model="addform.bindUser" placeholder="请输入转让人" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="求租区域" prop="region">
          <el-input v-model="addform.region" placeholder="请输入求租区域" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="addform.telephone" placeholder="请输入电话" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="录入方式" prop="mode">
          <el-input v-model="addform.mode" placeholder="请输入录入方式" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="信息标题" prop="tittle">
          <el-input v-model="addform.tittle" placeholder="请输入信息标题" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="addform.address" placeholder="请输入详细地址" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="addform.status" size="medium">
            <el-radio v-for="(item, index) in StatusOptions" :key="index" :label="item.value"
            :disabled="item.disabled">{{item.label}}</el-radio></el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="resetForm = false">重 置</el-button>
          <el-button type="primary" @click="submitForm">提 交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addSell,showSell,updateSell,getSell,delSell } from "../../api/sell.js";

export default {
  name: "SellTable",
  data() {
    return {
      query: {
        bindUser: "",
        region: "",
        status: 1,
        pageIndex: 1,
        pageSize: 4,
      },
      options: [
        {
          value: "1",
          label: "已售",
        },
        {
          value: "0",
          label: "未售",
        },
      ],
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      resetForm: false,
      pageTotal: 0,
      addform: {
        bindUser: "",
        region: "",
        telephone: "",
        mode: "",
        tittle: "",
        status: "",
      },
      form: {
        bindUser: "",
        region: "",
        telephone: "",
        mode: "",
        address: "",
        tittle: "",
        status: "",
        createdDate: "",
        modifiedDate: "",
      },
      StatusOptions: [{
        "label": "离职",
        "value": 0
      }, {
        "label": "在职",
        "value": 1
      }],
      data() {
        return {
          options: [],
          value: "",
        };
      },
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.Show();
  },
  methods: {
    fgf(row) {
      if (row.status == "1") {
        return <span style="color:green">已售</span>;
      } else {
        return <span style="color:red">未售</span>;
      }
    },
    Show() {
      showSell({
          PageSize: this.query.pageSize,
          PageIndex: this.query.pageIndex,
          BindUser: this.query.bindUser,
          Region: this.query.region,
          Status: this.query.status,
      }).then((res) => {
        console.log(res.data);
        this.tableData = res.data.pagedData;
        this.pageTotal = res.data.totalCount;
      });
    },
   
    // 触发搜索按钮
    handleSearch() {
      this.Show();
    },
    //状态显示
    statusShow(value) {
      if (value == 1) {
        this.query.status = 1;
        this.Show();
      } else {
        this.query.status = 0;
        this.Show();
      }
    },
     // 删除操作
     handleDelete(index) {
       // 二次确认删除
       this.$confirm("确定要删除吗？", "提示", {
         type: "warning",
       })
         .then(() => {
           this.$message.success("删除成功");
          this.tableData.splice(index, 1);
         })
         .catch(() => {});
     },
    
    //删除操作
    open(row) {
      delSell({
        id:row.id
      }).then((res) => {
        console.log(res);
        if (res.data.code == 1) {
          alert("删除成功");
          this.Show();
        } else {
          alert("删除失败");
          this.Show();
        }
      });
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    //添加操作
    addSearch(){
      this.resetForm = true;
    },
    // 保存添加
    submitForm(){
      addSell(this.addform).then((res) => {
        if(res.code > 0)
        {
           alert("添加成功");
           this.Show();
        }
        else
        {
          alert("添加失败");
        }
      });
      this.resetForm = false;
    },
    // 编辑操作
    handleEdit(index, row) {
      getSell({
        id:row.id
      }).then((res) => {
        console.log(res);
        this.form = res.data;
      });
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      updateSell(this.form).then((res) => {
        if (res.code > 0) {
          alert("修改成功");
          this.Show();
        } else {
          alert("修改失败");
        }
      });
      this.editVisible = false;
    },
    // 分页导航
    handleSizeChange(pageSize) {
      this.query.pageSize = pageSize;
      this.Show();
    },
    handleCurrentChange(pageIndex) {
      this.query.pageIndex = pageIndex;
      this.Show();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
